require 'date'


class Entry
  
  attr_reader :date, :layer, :set, :phrase, :orientation, :sequence,
    :duration, :typedSequence
    
  attr_accessor :uid  
  
  # Create a new Data Entry from a String s where
  # s is in the following format: Date|uid|taskno|vertical/horizontal|key sequence
  def initialize(s) 
    entry = s.split('|')
    @date = _process_date(entry[0])
    @uid = entry[1].to_i
    @layer, @set, @phrase = _process_taskno(entry[2])
    @orientation = entry[3]
    @sequence = _process_sequence(entry[4])
    @duration = @sequence.inject(0) do |sum, item| 
                    sum + item[1] unless item[1].nil?
                end
    @typedSequence = @sequence.inject("") {|s, item| s + item[0] }                
  end
  
  
  private
  
  def _process_date(date)
    date[12,0] = "20"
    DateTime.parse(date) 
  end
  
  def _process_taskno(taskno)
    layer = taskno[0,1].to_i
    set_no = taskno[1,1].to_i
    phrase_no = taskno[2,2].to_i
    return layer, set_no, phrase_no
  end  
  
  def _process_sequence(sequence)
    s = sequence.split
    i = 0
    seq = []
    while (i < s.length)
      seq << [s[i].to_i.chr, s[i+1].to_i]
      i = i + 2
    end
    return seq  
  end
end